<?php

class Guild extends Core {
	public function get($name, $field = null) {
		$rs = $this->Database->query('SELECT
			Guild.G_Name AS name,
			Guild.G_Score AS score,
			Guild.G_Master AS master,
			Guild.G_Mark AS mark,
			Guild.G_Notice AS notice,
			Guild.G_Rival AS rival,
			Guild.G_Union AS aliance,
			Guild.Number AS id,
			(SELECT COUNT(*) FROM GuildMember WHERE GuildMember.G_Name = Guild.G_Name) AS count_members
			FROM Guild
			WHERE Guild.G_Name = %s
		', $name)->fetch();
		$data = array();
		if (!empty($rs)) {
			$data = $rs;
			$data['notice'] = htmlentities($data['notice']);
			$data['master'] = $this->Character->get($data['master']);
			$data['score'] = is_null($data['score']) ? 0 : $data['score'];
			$data['mark'] = bin2hex($data['mark']);
			$data['assistant'] = $this->get_assistant($data['name']);
			$data['batle_master'] = $this->get_batle_master($data['name']);			
			$data['members'] = $this->members($data['name']);
		}
		return is_null($field) ? $data : $data[$field];
	}

	public function members($guild) {
		$rs = $this->Database->query('SELECT 
			Name AS name 
			FROM GuildMember 
			WHERE G_Name = %s
		', $guild)->fetch_all();
		$data = array();
		foreach ($rs as $key => $value) {
			$data[$key] = $this->Character->get($value['name']);
		}
		return $data;
	}
	
	public function castle_siege() {
		$rs = $this->Database->query('SELECT 
			MuCastle_DATA.MAP_SVR_GROUP AS server_group, 
			MuCastle_DATA.SIEGE_START_DATE AS siege_start, 
			MuCastle_DATA.SIEGE_END_DATE AS siege_end, 
			MuCastle_DATA.SIEGE_GUILDLIST_SETTED AS guild_list_setted, 
			MuCastle_DATA.SIEGE_ENDED AS ended, 
			MuCastle_DATA.CASTLE_OCCUPY AS occupy, 
			MuCastle_DATA.OWNER_GUILD AS guild, 
			MuCastle_DATA.MONEY AS money, 
			MuCastle_DATA.TAX_RATE_CHAOS AS rate_chaos, 
			MuCastle_DATA.TAX_RATE_STORE AS rate_store, 
			MuCastle_DATA.TAX_HUNT_ZONE AS hunt_zone,
			Guild.G_Master AS master,
			Guild.G_Mark AS mark 
			FROM MuCastle_DATA
			INNER JOIN Guild ON (MuCastle_DATA.OWNER_GUILD = Guild.G_Name COLLATE DATABASE_DEFAULT)
		')->fetch_all();
		$data = array();
		foreach ($rs as $key => $value) {
			$data = $value;
			$data['mark'] = bin2hex($data['mark']);
			$data['siege_start'] = date('d-m-Y H:i:s', strtotime($data['siege_start']));
			$data['siege_end'] = date('d-m-Y H:i:s', strtotime($data['siege_end']));
			$data['guilds'] = $this->connected_on_siege();
		}
		return $data; 
	}
	
	protected function connected_on_siege($top = 50) {
		$rs = $this->Database->query('SELECT TOP ' . $top . ' 
			REG_SIEGE_GUILD AS guild, REG_MARKS as marks FROM MuCastle_REG_SIEGE
			ORDER BY REG_MARKS DESC, REG_SIEGE_GUILD ASC
		')->fetch_all();
		$data = array();
		foreach ($rs as $key => $value) {
			$data[$key] = $this->get($value['guild']);
			$data[$key]['marks'] = $value['marks'];
		}
		return $data;
	}
	
	protected function get_assistant($guild) {
		$rs = $this->Database->query('SELECT TOP 1 
			Name as character 
			FROM GuildMember 
			WHERE G_Status = 64 AND 
			G_Name = %s
		', $guild)->fetch();
		$data = array();
		if (!empty($rs)) {
			$data = $this->Character->get($rs['character']);
		}
		return $data;
	}
	
	protected function get_batle_master($guild) {
		$rs = $this->Database->query('SELECT 
			Name as character
			FROM GuildMember 
			WHERE G_Status = 32 AND 
			G_Name = %s
		', $guild)->fetch_all();
		$data = array();
		foreach ($rs as $key => $value) {
			$tmp = $this->Character->get($value['character']);
			if (!empty($tmp)) {
				$data[] = $tmp;
			}
		}
		return $data;
	}
}

?>